﻿#region Imports
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
#endregion Imports

namespace CommonUtilities.Controls
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class CUtil_TitleBar : UserControl
    {
        #region Command declarations
        public ICommand MinimizeCommand { get; set; }
        #endregion Command declarations

        #region Constructors
        public CUtil_TitleBar()
        {
            InitializeComponent();
            this.WindowState = WindowState.Normal;
        }
        #endregion Constructors

        #region Properties
        private Window parentWindow;
        public Window ParentWindow
        {
            get { return parentWindow; }
            set
            {
                parentWindow = value;

                imgLogo.Source = parentWindow.Icon;
                txtbTitle.Text = parentWindow.Title;

                this.WindowState = parentWindow.WindowState;
                UpdateUI();
            }
        }

        public WindowState WindowState { get; set; }
        #endregion Properties

        #region Events
        private void btnMinimize_Click(object sender, RoutedEventArgs e)
        {
            parentWindow.WindowState = WindowState.Minimized;
        }

        private void btnMaxRestore_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = parentWindow.WindowState = (parentWindow.WindowState == WindowState.Maximized) ? WindowState.Normal : WindowState.Maximized;
            UpdateUI();
        }

        private void UpdateUI()
        {
            if (this.WindowState == WindowState.Maximized)
            {
                btnRestore.Visibility = Visibility.Visible;
                btnMaximize.Visibility = Visibility.Collapsed;
            }
            else
            {
                btnRestore.Visibility = Visibility.Collapsed;
                btnMaximize.Visibility = Visibility.Visible;
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
        #endregion Events
    }
}
